package com.yulinlin.jdbc.serialize.impl;

import com.yulinlin.jdbc.serialize.ListString;
import com.yulinlin.lang.util.GenericUtil;

import java.lang.reflect.Field;


public class ListStringSerialize implements ITypeSerialize<ListString,String> {

    private static String splitStr="/";

    public String serialize (ListString coll) {
        StringBuffer sb = new StringBuffer();
        for (Object s : coll) {
            if(sb.length() > 0){
                sb.append(splitStr);
            }
            sb.append(s);
        }
        return sb.toString();
    }


    @Override
    public ListString unSerialize(String o,Field f) {
        ListString list = new ListString();
        Class clazz = GenericUtil.getFieldGeneric(f,0);

        for (String s : o.split(splitStr)) {
            if(s.isEmpty()){
                continue;
            }
            Object v = s;
            try {
                if(clazz == Integer.class){
                    v = Integer.parseInt(s);
                }else if(clazz == Long.class){
                    v = Long.parseLong(s);
                }else if(clazz == Double.class){
                    v = Double.parseDouble(s);
                }else if(clazz == String.class){

                }
            }catch (Exception e){
                throw new RuntimeException("值转换失败,请检查字段泛型:"+s);
            }

            list.add(v);
        }
        return list;
    }
}
